// CompareFileDialog.cpp : implementation file
//

#include "stdafx.h"
#include "FileArchive.h"
#include "CompareFileDialog.h"


// CCompareFileDialog dialog

extern CString		g_archivePath;

BOOL SetClipboardText(HWND, LPCTSTR pszText);

IMPLEMENT_DYNAMIC(CCompareFileDialog, CDialog)

CCompareFileDialog::CCompareFileDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CCompareFileDialog::IDD, pParent)
{

}

CCompareFileDialog::~CCompareFileDialog()
{
}

void CCompareFileDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_ARCHIVED_FILE, m_listArchivedFile);
	DDX_Control(pDX, IDC_EDIT_SELECTED_FILE, m_editCurrentFile);
}

void	CCompareFileDialog::SetFilename(CString &strFilename)
{
	m_strFilename = strFilename;
}

BEGIN_MESSAGE_MAP(CCompareFileDialog, CDialog)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_ARCHIVED_FILE, &CCompareFileDialog::OnLvnItemchangedListArchivedFile)
	ON_BN_CLICKED(IDC_BUTTON_COPY_FULL_PATH, &CCompareFileDialog::OnBnClickedButtonCopyFullPath)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_FILE, &CCompareFileDialog::OnBnClickedButtonOpenFile)
	ON_BN_CLICKED(IDC_BUTTON_RECOVER, &CCompareFileDialog::OnBnClickedButtonRecover)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_FOLDER, &CCompareFileDialog::OnBnClickedButtonOpenFolder)
END_MESSAGE_MAP()

// CCompareFileDialog message handlers
void CCompareFileDialog::OnLvnItemchangedListArchivedFile(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: Add your control notification handler code here

	ButtonEnable();

	*pResult = 0;
}

void CCompareFileDialog::ButtonEnable()
{
	POSITION pos = m_listArchivedFile.GetFirstSelectedItemPosition();
	if ( pos == NULL )
	{
		GetDlgItem(IDC_BUTTON_COPY_FULL_PATH)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_OPEN_FILE)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_RECOVER)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_OPEN_FOLDER)->EnableWindow(FALSE);
	}
	else
	{
		GetDlgItem(IDC_BUTTON_COPY_FULL_PATH)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_OPEN_FILE)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_RECOVER)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_OPEN_FOLDER)->EnableWindow(TRUE);
	}
}

BOOL CCompareFileDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	TCHAR _drive[MAX_PATH];
	TCHAR _dir[MAX_PATH];
	TCHAR _fname[MAX_PATH];
	TCHAR _ext[MAX_PATH];

	_tsplitpath(m_strFilename, _drive, _dir, _fname, _ext);	

	CString drive(_drive);
	CString dir(_dir);
	CString fname(_fname);
	CString ext(_ext);

	m_strSourceFilePath.Format(_T("%s%s"), drive, dir);

	//
	m_editCurrentFile.ReplaceSel(m_strFilename);
	
	m_listArchivedFile.SetExtendedStyle(m_listArchivedFile.GetExtendedStyle()|LVS_EX_FULLROWSELECT);

	m_listArchivedFile.InsertColumn(0, _T("Time"),LVCFMT_LEFT, 100);
	m_listArchivedFile.InsertColumn(1, _T("Label"),LVCFMT_LEFT, 100);
	m_listArchivedFile.InsertColumn(2, _T("Full Filename"),LVCFMT_LEFT, 200);
	
	//
	m_strArchivedPath.Format(_T("%s%s%s\\"), drive, dir, g_archivePath );

	//
	int iItem = 0;
	CString archiveFilename;

	archiveFilename.Format( _T( "%s*.*" ), m_strArchivedPath );

	int colWidth[3] = {0,0,0};

	CFileFind finder;
	BOOL bWorking = finder.FindFile(archiveFilename);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		CString filename = finder.GetFileName();

		//	example: {2010_03_17_22_55_35}(Label)test1.txt
		if ( filename.Right((fname+ext).GetLength()) == fname+ext )
		//	if ( filename.Mid(CString(_T("{2010_03_17_22_55_35}(ver_001)")).GetLength()) == fname+ext )
		{
			if ( filename[0] == '{' && filename[20] == '}' && filename[21] == '(' )
			{
				CString date = filename.Mid(1,10);
				date.Replace('_', '-');
				CString time = filename.Mid(12,8);
				time.Replace('_', ':');

				int endLabel = filename.Find(')', 22);
				if ( endLabel != -1 )
				{
					CString strLabel = filename.Mid(22, endLabel-22);

					CString strDateTime = date+_T(" ")+time;
					m_listArchivedFile.InsertItem(iItem, strDateTime);
					colWidth[0] = max(colWidth[0], m_listArchivedFile.GetStringWidth(strDateTime) );

					m_listArchivedFile.SetItemText(iItem, 1, strLabel );
					colWidth[1] = max(colWidth[1], m_listArchivedFile.GetStringWidth(strLabel) );

					m_listArchivedFile.SetItemText(iItem, 2, filename);
					colWidth[2] = max(colWidth[2], m_listArchivedFile.GetStringWidth(filename) );

					iItem ++;
				}
			}
		}
	}

	//	list ctrl column Width
	m_listArchivedFile.SetColumnWidth(0, colWidth[0]+15);
	m_listArchivedFile.SetColumnWidth(1, colWidth[1]+15);
	m_listArchivedFile.SetColumnWidth(2, colWidth[2]+15);

	//
	ButtonEnable();
	
	if ( iItem == 0 )
	{
		AfxMessageBox(_T("There are no archived files."));
		CDialog::OnCancel();
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CCompareFileDialog::OnBnClickedButtonCopyFullPath()
{
	int selectedPos = GetSelectedPos();
	if ( selectedPos != -1 )
	{
		CString strFilename = m_strArchivedPath + m_listArchivedFile.GetItemText(selectedPos, 2);
		//	copy Clipboard.
		SetClipboardText(GetSafeHwnd(), strFilename);
	}
}

void CCompareFileDialog::OnBnClickedButtonOpenFile()
{
	int selectedPos = GetSelectedPos();
	if ( selectedPos != -1 )
	{
		CString strFilename = m_strArchivedPath + m_listArchivedFile.GetItemText(selectedPos, 2);
		ShellExecute(NULL, _T("open"), strFilename , NULL, NULL, SW_SHOW);
	}
}

void CCompareFileDialog::OnBnClickedButtonRecover()
{
	int selectedPos = GetSelectedPos();
	if ( selectedPos != -1 )
	{
		CString strFilename = m_listArchivedFile.GetItemText(selectedPos, 2);
		CString strArchivedFilename = m_strArchivedPath + m_listArchivedFile.GetItemText(selectedPos, 2);

		//	copy upper folder.
		BOOL bRet = CopyFile( strArchivedFilename, m_strSourceFilePath+strFilename, FALSE ); 
		if ( bRet == 0 )
		{
			AfxMessageBox(_T("Recover Failed"));
		}
	}
}

void CCompareFileDialog::OnBnClickedButtonOpenFolder()
{
	int selectedPos = GetSelectedPos();
	if ( selectedPos != -1 )
	{
		CString strFilename = m_strArchivedPath + m_listArchivedFile.GetItemText(selectedPos, 2);

		TCHAR _drive[MAX_PATH];
		TCHAR _dir[MAX_PATH];
		TCHAR _fname[MAX_PATH];
		TCHAR _ext[MAX_PATH];

		_tsplitpath(strFilename, _drive, _dir, _fname, _ext);	

		CString drive(_drive);
		CString dir(_dir);
		CString fname(_fname);
		CString ext(_ext);

		ShellExecute(NULL, _T("explore"), drive+dir, NULL, NULL, SW_SHOW);
	}
}

int CCompareFileDialog::GetSelectedPos()
{
	POSITION pos = m_listArchivedFile.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		return -1;
	}
	else
	{
		while (pos)
		{
			int nItem = m_listArchivedFile.GetNextSelectedItem(pos);
			return nItem;
		}
	}
	return -1;
}

BOOL SetClipboardText(HWND hWnd, LPCTSTR pszText)
{
	USES_CONVERSION;

	BOOL ok = FALSE;
	if(OpenClipboard(hWnd)) 
	{
		EmptyClipboard();

		// the text should be placed in "global" memory
		HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (lstrlen(pszText)+1)*sizeof(TCHAR) );
		LPTSTR ptxt = (LPTSTR)GlobalLock(hMem);
		lstrcpy(ptxt, pszText);
		GlobalUnlock(hMem);
		// set data in clipboard; we are no longer responsible for hMem
		ok = (BOOL)SetClipboardData(CF_UNICODETEXT, hMem);

		CloseClipboard(); // relinquish it for other windows
	}
	return ok;
}

